<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <!-- 放置分页组件 -->
              <el-pagination
                :current-page.sync="pageParams.page"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="pageParams.pagesize"
                layout="sizes, prev, pager, next"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <button @click="fangwen">访问abc页面</button>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog
        :title="1 ? '编辑' : '添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showAssign"
      >
        <assignPermission ref="refAssign" :role-id="roleId" @close="hCloses" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, deleteRole, addRoles, updateRole } from '@/api/setting'
// 引入子组件
import assignPermission from './assignPermission'
export default {
  components: { assignPermission },
  data() {
    return {
      roleId: '',
      showAssign: false,
      list: [], // 存储角色列表
      total: 0,
      pageParams: {
        page: 1, // 当前页
        pagesize: 2 // 每页几条
      },
      isEdit: false, // /判断是编辑还是添加
      showDialog: false, // 控制弹框
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '描述信息不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 点击分配权限
    hAssign(id) {
      this.showAssign = true
      this.roleId = id
      this.$nextTick(() => {
        this.$refs.refAssign.loadPermissionByRoleId()
      })
    },
    // 获取所有角色列表
    async loadRoles() {
      const { data: res } = await getRoles(this.pageParams)
      console.log('角色列表', res)
      this.list = res.rows
      this.total = res.total
    },

    hCurrentChange(curPage) {
      // 每页几条数据
      this.pageParams.page = curPage
      this.loadRoles()
    },
    handleSizeChange(val) {
      // console.log(val)
      this.pageParams.pagesize = val
      this.loadRoles()
    },
    // 点击删除
    async doDel(id) {
      try {
        await deleteRole(id)
        // console.log(res);
        if (this.list.length === 1 && this.pageParams.page !== 1) {
          this.pageParams.page--
        }
        // 1. 提示
        this.$message.success('删除成功')
        // 2. 重新请求
        this.loadRoles()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },

    // 用户点击了删除
    hDel(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
    },
    // 点击添加
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },

    // 点击确定添加新增角色
    async addRole() {
      try {
        await addRoles(this.roleForm)
        // 让页码跳转到最后一页
        this.total++
        this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
        this.$message.success('添加成功')
        // 重新渲染页面
        this.loadRoles()
        // 关闭弹框
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
        console.log('添加失败', err)
      }
    },
    // 点击确定
    hSubmit() {
      //  兜底校验
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.enterEdit() : this.addRole()
        }
      })
    },

    // 点击确定编辑
    async enterEdit() {
      await updateRole(this.roleForm) // 发送请求
      // 关闭弹框
      this.showDialog = false
      // 重新渲染
      this.loadRoles()
      // 提示信息
      this.$message.success('编辑成功')
    },
    // 点击编辑
    hEdit(scope) {
      console.log(scope)
      this.showDialog = true
      // 数据回填
      this.roleForm = { ...scope }
      this.isEdit = true
    },
    // 弹框关闭触发的事件
    hClose() {
      this.$refs.roleForm.resetFields()
    },
    hCloses() {
      this.showAssign = false
    },
    fangwen() {
      this.$router.addRoutes([
        {
          path: '/abc',
          // 路由懒加载   只有当访问了login时 才会加载(下载资源)组件,('@/views/login/index')
          component: () => import('@/views/settings/abc.vue'),
          hidden: true
        }
      ])
    }
  }
}
</script>
